#include "StdAfx.h"
#include "CHGlobal.h"
#include "CHColorRef.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CHGlobal CHGlobalData;

static const TCHAR _xtOfficeFaceName[]	= _T("Tahoma");
static const TCHAR _xtVertFaceName[]	= _T("Arial");

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CHGlobal::CHGlobal()
{
	LoadOSVersion();
	LoadSysColors();
	LoadSysFonts();
}

CHGlobal::~CHGlobal()
{
	FreeSysFonts();
}

static int CALLBACK EnumFontFamExProc(ENUMLOGFONTEX* pelf,
	NEWTEXTMETRICEX* lpntm, int FontType, LPVOID pThis)
{
	UNUSED_ALWAYS(lpntm);
	UNUSED_ALWAYS(FontType);
	UNUSED_ALWAYS(pThis);

	CString strFaceName(pelf->elfLogFont.lfFaceName);
	return strFaceName.Compare(_xtOfficeFaceName);
}

bool CHGlobal::FontExists(CString &strFaceName)
{
	LOGFONT lfEnum;
	lfEnum.lfFaceName[ 0 ] = 0;
	lfEnum.lfCharSet = DEFAULT_CHARSET;

	CWindowDC dc( NULL );
	if ( ::EnumFontFamiliesEx( dc.m_hDC, &lfEnum, ( FONTENUMPROC )EnumFontFamExProc, 
								( LPARAM )&strFaceName, 0 ) == 0 )
	{
		return true;
	}

	return false;
}

void CHGlobal::LoadSysFonts()
{
	// Free up any resources previously allocated.
	FreeSysFonts();

	CString strHorzFont = _T( "Tahoma" );
	CString strVertFont = _T( "Arial" );

	// Set the character set to default.
	NONCLIENTMETRICS ncm;
	memset(&ncm, 0, sizeof(NONCLIENTMETRICS));
	ncm.cbSize = sizeof(NONCLIENTMETRICS);
	::SystemParametersInfo(SPI_GETNONCLIENTMETRICS,	sizeof(NONCLIENTMETRICS), &ncm, 0);

	LOGFONT lfHorz;
	::ZeroMemory(&lfHorz, sizeof(LOGFONT));
	::CopyMemory((void*)&lfHorz, (const void*)&(ncm.lfMenuFont), (DWORD)sizeof(LOGFONT));
	LOGFONT lfVert;
	::ZeroMemory(&lfVert, sizeof(LOGFONT));
	::CopyMemory((void*)&lfVert, (const void*)&(ncm.lfMenuFont), (DWORD)sizeof(LOGFONT));

	if ( FontExists( strHorzFont ) )
	{
		_tcscpy( lfHorz.lfFaceName, strHorzFont );
	}
	else
	{
		ASSERT( FALSE );
	}
	
	if ( FontExists( strVertFont ) )
	{
		_tcscpy( lfVert.lfFaceName, strVertFont );
	}
	else
	{
		ASSERT( FALSE );
	}

	CreateSysFont( lfHorz, fontBold, FW_BOLD );
	CreateSysFont( lfHorz, font );
	CreateSysFont( lfHorz, fontULine, FW_NORMAL, 1 );
	CreateSysFont( lfHorz, fontHCapt );
	CreateSysFont( lfVert, fontVCapt, FW_NORMAL, 0, 900, 900 );
	CreateSysFont( lfVert, fontVCaptBold, FW_BOLD, 0, 900, 900 );
	CreateSysFont( lfVert, fontVCaptR, FW_NORMAL, 0, 2700, 2700 );
	CreateSysFont( lfVert, fontVCaptRBold, FW_BOLD, 0, 2700, 2700 );
}

void CHGlobal::FreeSysFonts()
{
	font.DeleteObject();
	fontBold.DeleteObject();
}

void CHGlobal::LoadSysColors()	
{
	CHColorRef::RefreshColors();

	// Initialize standard color for windows components.
	clr3DFace                   = CHColorRef::GetColor( COLOR_3DFACE );
	clr3DShadow					= CHColorRef::GetColor( COLOR_3DSHADOW );
	clr3DDkShadow				= CHColorRef::GetColor( COLOR_3DDKSHADOW );
	clr3DHilight				= CHColorRef::GetColor( COLOR_3DHILIGHT );
	clrBtnText					= CHColorRef::GetColor( COLOR_BTNTEXT );
	clrGrayText					= CHColorRef::GetColor( COLOR_GRAYTEXT );
	clrHighlight				= CHColorRef::GetColor( COLOR_HIGHLIGHT );
	clrHighlightText			= CHColorRef::GetColor( COLOR_HIGHLIGHTTEXT );
	clrMenu						= CHColorRef::GetColor( COLOR_MENU );
	clrMenuText					= CHColorRef::GetColor( COLOR_MENUTEXT );
	clrWindow					= CHColorRef::GetColor( COLOR_WINDOW );
	clrWindowFrame				= CHColorRef::GetColor( COLOR_WINDOWFRAME );
	clrWindowText				= CHColorRef::GetColor( COLOR_WINDOWTEXT );
	clrActiveCaption		    = CHColorRef::GetColor( COLOR_ACTIVECAPTION );
	clrInActiveCaption          = CHColorRef::GetColor( COLOR_INACTIVECAPTION );
	clrGradActiveCapt           = CHColorRef::GetColor( COLOR_GRADIENTACTIVECAPTION );
	clrGradInActiveCapt         = CHColorRef::GetColor( COLOR_GRADIENTINACTIVECAPTION );
	clrActiveCaptText           = CHColorRef::GetColor( COLOR_CAPTIONTEXT );
	clrInactiveCaptText         = CHColorRef::GetColor( COLOR_INACTIVECAPTIONTEXT );

	// For some strange reason Windows 2000/NT does not return
	// the expected RGB value for COLOR_3DLIGHT...
	clr3DLight					= CHColorRef(clr3DFace) + 15;

	// Initialize special colors for XP style interfaces.
	clrXPBarFace				= CHColorRef::GetColor( XPCOLOR_BARFACE );
	clrXPHighlight				= CHColorRef::GetColor( XPCOLOR_HIGHLIGHT );
	clrXPHighlightBorder		= CHColorRef::GetColor( XPCOLOR_HIGHLIGHT_BORDER );
	clrXPHighlightPushed		= CHColorRef::GetColor( XPCOLOR_HIGHLIGHT_PUSHED );
	clrXPIconShadow				= CHColorRef::GetColor( XPCOLOR_ICONSHADDOW );
	clrXPGrayText				= CHColorRef::GetColor( XPCOLOR_GRAYTEXT );
	clrXPHighlightChecked		= CHColorRef::GetColor( XPCOLOR_HIGHLIGHT_CHECKED );
	clrXPHighlightCheckedBorder	= CHColorRef::GetColor( XPCOLOR_HIGHLIGHT_CHECKED_BORDER );
	clrXPGripper				= CHColorRef::GetColor( XPCOLOR_GRIPPER );
	clrXPSeparator				= CHColorRef::GetColor( XPCOLOR_SEPARATOR );
	clrXPDisabled				= CHColorRef::GetColor( XPCOLOR_DISABLED );
	clrXPMenuTextBack			= CHColorRef::GetColor( XPCOLOR_MENUTEXT_BACK );
	clrXPMenuExpanded           = CHColorRef::GetColor( XPCOLOR_MENU_EXPANDED );
	clrXPMenuBorder				= CHColorRef::GetColor( XPCOLOR_MENU_BORDER );
	clrXPMenuText				= CHColorRef::GetColor( XPCOLOR_MENUTEXT );
	clrXPHighlightText			= CHColorRef::GetColor( XPCOLOR_HIGHLIGHT_TEXT );
	clrXPBarText				= CHColorRef::GetColor( XPCOLOR_BARTEXT );
	clrXPBarTextPushed			= CHColorRef::GetColor( XPCOLOR_BARTEXT_PUSHED );
	clrXPTabInactiveBack		= CHColorRef::GetColor( XPCOLOR_TAB_INACTIVE_BACK );
	clrXPTabInactiveText		= CHColorRef::GetColor( XPCOLOR_TAB_INACTIVE_TEXT );
}

void CHGlobal::LoadOSVersion()
{
	bWin95 = FALSE;
	bWin98 = FALSE;
	bWinME = FALSE;
	bWinNT = FALSE;
	bWin2K = FALSE;
	bWinXP = FALSE;

	OSVERSIONINFO osvi;
	::ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	::GetVersionEx (&osvi);

	bWin95 =(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) && 
			(osvi.dwMajorVersion == 4) && 
			(osvi.dwMinorVersion < 10);

	bWin98 =(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) && 
			(osvi.dwMajorVersion == 4) && 
			(osvi.dwMinorVersion == 10);

	bWinME =(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) && 
			(osvi.dwMajorVersion == 4) && 
			(osvi.dwMinorVersion == 90);

	bWinNT =(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) && 
			(osvi.dwMajorVersion == 4) && 
			(osvi.dwMinorVersion == 0);

	bWin2K =(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) && 
			(osvi.dwMajorVersion == 5) && 
			(osvi.dwMinorVersion == 0);

	bWinXP =(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) && 
			(osvi.dwMajorVersion == 5) && 
			(osvi.dwMinorVersion == 1);
}



bool CHGlobal::CreateSysFont(LOGFONT &lf, CFont &font, long lfWeight, char lfUnderline, long lfOrientation, long lfEscapement)
{
	lf.lfWeight      = lfWeight;
	lf.lfOrientation = lfOrientation;
	lf.lfEscapement  = lfEscapement;
	lf.lfUnderline   = lfUnderline;

	if ( !font.CreateFontIndirect( &lf ) )
	{
		return false;
	}

	return true;
}
